package org.tlipoca.smcomment.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.genntii.smdomain.entity.Comment;

import java.util.ArrayList;

@Mapper
public interface CommentMapper extends BaseMapper<Comment> {

    @Select("select count(*) from Comment where reply_comment_id = #{commentId}")
    Integer replyExists(@Param("commentId") Long commentId);

    @Select("select * from Comment where reply_comment_id = #{commentId}")
    ArrayList<Comment> getReplyComment(@Param("commentId") Long commentId);

    @Select("select * from Comment where article_id = #{articleId} and reply_comment_id is null limit #{pageNum},#{pageSize};")
    ArrayList<Comment> selectCommentListByArticleId(@Param("articleId") Long articleId,
                                                    @Param("pageNum")Integer pageNum,
                                                    @Param("pageSize")Integer pageSize);

    @Select("select count(*) from Comment where article_id = #{articleId}")
    long selectCommentTotalByArticleId(@Param("articleId")Long articleId);

    @Select("select * from Comment where user_id = #{userId} ")
    ArrayList<Comment> getCommentByUserId(@Param("userId") Long userId);

    @Select("select count(*) from Comment where reply_comment_id = #{commentId}")
    Integer getReplyCount(@Param("commentId")Long commentId);

    @Select("""
            SELECT *\s
            FROM `Comment`.`Comment` as c\s
            LEFT JOIN Comment_Like.Comment_Like as cl\s
            ON c.id = cl.comment_id\s
            WHERE c.reply_comment_id = 5\s
            ORDER BY cl.`like` DESC\s
            LIMIT 3;""")
    ArrayList<Comment> get3Reply(@Param("replyCommentId") Long replyCommentId);

    @Select("select * from Comment where reply_comment_id = #{commentId}")
    ArrayList<Comment> getAllReply(@Param("commentId")Long commentId);

    @Select("select count(*) from Comment where article_id = #{articleId}")
    Integer getArticleCommentCount(@Param("articleId") Long articleId);

    @Select("select * from Comment " +
            "where user_id = #{userId} " +
            "order by update_time desc " +
            "limit #{pageNum}, #{pageSize}")
    ArrayList<Comment> getCommentByUserLimit(@Param("userId")Long userId,
                                             @Param("pageNum") Integer pageNum,
                                             @Param("pageSize") Integer pageSize);

    @Select("select count(*) from Comment where user_id = #{userId}")
    Long getCommentTotalByUserId(@Param("userId")Long userId);
}
